﻿#include "Server.MirForms.VisualMapInfo.Control.MineEntry.h"
#include "../Class/VisualizerGlobal.h"
#include "../../../Server/Settings.h"
#include "Forms/Server.MirForms.VisualMapInfo.Control.Forms.MiningDetailForm.h"

using namespace Microsoft::VisualBasic::PowerPacks;
using namespace Server::MirForms::VisualMapInfo::Class;

namespace Server::MirForms::VisualMapInfo::Control
{

    unsigned short MineEntry::getRange() const
    {
        return static_cast<unsigned short>(tempRange);
    }

    void MineEntry::setRange(unsigned short value)
    {
        if (value > 0)
        {
            tempRange = static_cast<unsigned short>(value);
        }
        else
        {
            tempRange = 1;
        }

        RegionHighlight->Size = new Size((tempRange * 2) * VisualizerGlobal::ZoomLevel, (tempRange * 2) * VisualizerGlobal::ZoomLevel);

        RegionHighlight->Left = (X - value) * VisualizerGlobal::ZoomLevel;
        RegionHighlight->Top = (Y - value) * VisualizerGlobal::ZoomLevel;

        Details->Text = StringHelper::formatSimple("X: {0} | Y: {1} | Range: {2}", std::to_string(X), std::to_string(Y), std::to_string(getRange()));
    }

    CreateParams *MineEntry::getCreateParams() const
    {
        auto parms = UserControl::CreateParams;
        parms->Style &= ~0x02000000; // Turn off WS_CLIPCHILDREN
        return parms;
    }

    MineEntry::MineEntry()
    {
        InitializeComponent();
        InitializeRegionHighlight();
    }

    void MineEntry::InitializeRegionHighlight()
    {
        RegionHighlight->Visible = false;
        RegionHighlight->BorderColor = Color::Lime;
        RegionHighlight->FillColor = Color::FromArgb(30, 200, 0, 200);

        RegionHighlight->BorderWidth = 2;
        RegionHighlight->MouseMove += new System::Windows::Forms::MouseEventHandler(this->Region_MouseMove);
        RegionHighlight->MouseDown += new System::Windows::Forms::MouseEventHandler(this->Region_MouseDown);
        RegionHighlight->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        RegionHighlight->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        RegionHighlight->MouseClick += new System::Windows::Forms::MouseEventHandler(RegionHighlight_MouseClick);
        RegionHighlight->MouseHover += RegionHighlight_MouseHover;
    }

    void MineEntry::Region_MouseLeave(std::any sender, EventArgs *e)
    {
        if (RegionHighlight->Visible)
        {
            this->BackColor = Color::White;
        }
        else
        {
            this->BackColor = Color::Silver;
        }

        RegionHighlight->BorderColor = Color::Lime;
        RegionHighlight->FillStyle = FillStyle::Transparent;
    }

    void MineEntry::Region_MouseEnter(std::any sender, EventArgs *e)
    {
        if (!RegionHighlight->Visible)
        {
            return;
        }

        this->BackColor = Color::MediumOrchid;

        RegionHighlight->BorderColor = Color::MediumOrchid;
        RegionHighlight->FillStyle = FillStyle::Solid;
        RegionHighlight->Cursor = VisualizerGlobal::Cursor;
    }

    void MineEntry::Region_MouseDown(std::any sender, MouseEventArgs *e)
    {
        RegionHighlight->BringToFront();

        this->BackColor = Color::Orange;
        RegionHighlight->BorderColor = Color::Orange;

        if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Select)
        {
            return;
        }

        MouseDownLocation = e->Location;
    }

    void MineEntry::Region_MouseMove(std::any sender, MouseEventArgs *e)
    {
        if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Select)
        {
            return;
        }
        if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Add)
        {
            return;
        }

        if (e->Button == System::Windows::Forms::MouseButtons::Left)
        {
            if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Move)
            {
                RegionHighlight->Left = e->X + RegionHighlight->Left - MouseDownLocation->X;
                RegionHighlight->Top = e->Y + RegionHighlight->Top - MouseDownLocation->Y;

                X = (RegionHighlight->Left + getRange()) / VisualizerGlobal::ZoomLevel;
                Y = (RegionHighlight->Top + getRange()) / VisualizerGlobal::ZoomLevel;

                setRange(tempRange);
            }
            else if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Resize)
            {
                setRange(getRange() + static_cast<unsigned short>((MouseDownLocation->X - e->Location->X) / VisualizerGlobal::ZoomLevel));
            }
        }

        if (e->Button == System::Windows::Forms::MouseButtons::Right)
        {
            if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Move)
            {
                setRange(getRange() + static_cast<unsigned short>((MouseDownLocation->X - e->Location->X) / VisualizerGlobal::ZoomLevel));
            }
            else
            {
                return;
            }
        }
    }

    void MineEntry::RegionHighlight_MouseClick(std::any sender, MouseEventArgs *e)
    {
        if (VisualizerGlobal::SelectedTool == VisualizerGlobal::Tool::Focus)
        {
            VisualizerGlobal::SelectedFocusType = VisualizerGlobal::FocusType::Mining;
            VisualizerGlobal::FocusMineEntry = this;
            VisualizerGlobal::setFocusModeActive(true);
        }

        MineComboBox->Focus();
    }

    void MineEntry::RegionHighlight_MouseHover(std::any sender, EventArgs *e)
    {
        RegionHighlight->Cursor = VisualizerGlobal::Cursor;
    }

    void MineEntry::UpdateForFocus()
    {
        RegionHighlight->Left = X * VisualizerGlobal::ZoomLevel;
        RegionHighlight->Top = Y * VisualizerGlobal::ZoomLevel;
        setRange(tempRange);
    }

    void MineEntry::RemoveEntry()
    {
        RegionHighlight->Dispose();
        this->Dispose();
    }

    void MineEntry::HideControl()
    {
        RegionHighlight->Visible = false;
    }

    void MineEntry::ShowControl()
    {
        RegionHighlight->Visible = true;
    }

    void MineEntry::HideRegion()
    {
        RegionHighlight->Visible = false;
        this->BackColor = Color::Silver;
        RegionHidden = true;
    }

    void MineEntry::ShowRegion()
    {
        RegionHighlight->Visible = true;
        this->BackColor = Color::White;
        RegionHidden = false;
    }

    void MineEntry::MineComboBox_SelectedIndexChanged(std::any sender, EventArgs *e)
    {
        MineIndex = static_cast<unsigned char>(MineComboBox->SelectedIndex);
    }

    void MineEntry::MineEntry_Load(std::any sender, EventArgs *e)
    {
        ListItem *tempVar = new ListItem();
        tempVar->Text = "Disabled";
        tempVar->Value = "0";
        MineComboBox->Items->Add(tempVar);
        for (int i = 0; i < Settings::MineSetList.size(); i++)
        {
            ListItem tempVar2(Settings::MineSetList[i]->Name, std::to_string(i + 1));
            MineComboBox->Items->Add(&tempVar2);
        }

        MineComboBox->SelectedIndex = MineIndex;

        Details->Text = StringHelper::formatSimple("X: {0} | Y: {1} | Range: {2}", std::to_string(X), std::to_string(Y), std::to_string(getRange()));

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void MineEntry::Details_DoubleClick(std::any sender, EventArgs *e)
    {
        Forms::MiningDetailForm *MiningDetail = new Forms::MiningDetailForm();

        MiningDetail->X->Text = std::to_string(X);
        MiningDetail->Y->Text = std::to_string(Y);
        MiningDetail->Range->Text = std::to_string(getRange());

        MiningDetail->ShowDialog();

        X = StringHelper::fromString<short>(MiningDetail->X->Text);
        Y = StringHelper::fromString<short>(MiningDetail->Y->Text);
        setRange(StringHelper::fromString<unsigned short>(MiningDetail->Range->Text));

        MiningDetail->Dispose();

        delete MiningDetail;
    }

    void MineEntry::Dispose(bool disposing)
    {
        if (disposing && (components != nullptr))
        {
            components->Dispose();
        }
        UserControl::Dispose(disposing);
    }

    void MineEntry::InitializeComponent()
    {
        this->MineComboBox = new System::Windows::Forms::ComboBox();
        this->Details = new System::Windows::Forms::Label();
        this->Selected = new System::Windows::Forms::CheckBox();
        this->SuspendLayout();
        // 
        // MineComboBox
        // 
        this->MineComboBox->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
        this->MineComboBox->FormattingEnabled = true;
        this->MineComboBox->Location = new System::Drawing::Point(24, 3);
        this->MineComboBox->Name = "MineComboBox";
        this->MineComboBox->Size = new System::Drawing::Size(68, 21);
        this->MineComboBox->TabIndex = 2;
        this->MineComboBox->SelectedIndexChanged += new System::EventHandler(this->MineComboBox_SelectedIndexChanged);
        this->MineComboBox->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        // 
        // Details
        // 
        this->Details->AutoSize = true;
        this->Details->Location = new System::Drawing::Point(97, 7);
        this->Details->Name = "Details";
        this->Details->Size = new System::Drawing::Size(39, 13);
        this->Details->TabIndex = 3;
        this->Details->Text = "Details";
        this->Details->DoubleClick += new System::EventHandler(this->Details_DoubleClick);
        this->Details->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        // 
        // Selected
        // 
        this->Selected->AutoSize = true;
        this->Selected->Location = new System::Drawing::Point(3, 7);
        this->Selected->Name = "Selected";
        this->Selected->Size = new System::Drawing::Size(15, 14);
        this->Selected->TabIndex = 4;
        this->Selected->UseVisualStyleBackColor = true;
        this->Selected->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        // 
        // MineEntry
        // 
        this->AutoScaleDimensions = new System::Drawing::SizeF(6.0F, 13.0F);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->BackColor = System::Drawing::Color::White;
        this->Controls->Add(this->Selected);
        this->Controls->Add(this->Details);
        this->Controls->Add(this->MineComboBox);
        this->Name = "MineEntry";
        this->Size = new System::Drawing::Size(249, 28);
        this->Load += new System::EventHandler(this->MineEntry_Load);
        this->MouseEnter += new System::EventHandler(this->Region_MouseEnter);
        this->MouseLeave += new System::EventHandler(this->Region_MouseLeave);
        this->ResumeLayout(false);
        this->PerformLayout();

    }
}
